package com.sunday.haowushop.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.haowushop.R;
import com.sunday.haowushop.common.BaseApp;
import com.sunday.haowushop.entity.Member;
import com.sunday.haowushop.http.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/9/27.
 */

public class SettingPwdActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.et_phone)
    ClearEditText etPhone;
    @Bind(R.id.et_code)
    ClearEditText etCode;
    @Bind(R.id.send_code)
    TextView sendCode;
    @Bind(R.id.et_pwd)
    ClearEditText etPwd;
    @Bind(R.id.iv_reset_pwd_visible)
    ImageButton ivResetPwdVisible;
    @Bind(R.id.btn_register)
    TextView btnRegister;
    private TimeCount timeCount;
    private boolean fromOrderPay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_pwd);
        ButterKnife.bind(this);
        titleView.setText("设置支付密码");
        timeCount = new TimeCount(60000, 1000, sendCode);
        fromOrderPay=getIntent().getBooleanExtra("fromOrderPay",false);

    }

    @OnClick(R.id.btn_register)
    void onClick() {
        String code = etCode.getText().toString().trim();
        String pwd = etPwd.getText().toString().trim();
        String mobile = etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(code) || TextUtils.isEmpty(pwd) || TextUtils.isEmpty(mobile)) {
            return;
        }

        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().updatePayPwd(BaseApp.getInstance().getMember().getId(), code, MD5Utils.MD5(pwd));
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "设置成功");
                    if (fromOrderPay){
                        Intent data=new Intent();
                        setResult(RESULT_OK,data);
                    }
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    @OnClick(R.id.send_code)
    void sendCode() {
        String mobile = etPhone.getText().toString().trim();
        if (TextUtils.isEmpty(mobile) || !StringUtils.isMobileNO(mobile)) {
            ToastUtils.showToast(mContext, "请输入正确的手机号");
            return;
        }
        Call<ResultDO> call = ApiClient.getApiAdapter().sendCode(mobile, 3);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "验证码已发送到手机");
                    timeCount.start();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


    private boolean isPressed = true;

    @OnClick(R.id.iv_reset_pwd_visible)
    void set() {
        if (!isPressed) {
            ivResetPwdVisible.setSelected(false);
            etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            ivResetPwdVisible.setSelected(true);
        }
        isPressed = !isPressed;
        etPwd.postInvalidate();
        CharSequence text = etPwd.getText();
        if (text instanceof Spannable) {
            Spannable spanText = (Spannable) text;
            Selection.setSelection(spanText, text.length());
        }
    }
}
